<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/10/26
 * Time: 17:15
 */
class EntrustController extends Controller
{
    private $_title;
    private $_logininfo;
    private $_daytime;
    private $_daydate;

    public function init()
    {
        parent::init();
        $this->_logininfo = $this->_session->offsetGet('loginInfo');
        $__now_time = $_SERVER['REQUEST_TIME'];
        $this->_daytime = date("Y-m-d H:i:s",$__now_time);
        $this->_daydate = date("Y-m-d",$__now_time);
    }

    public function indexAction(){
        $this->_title ="快捷求购";
        $this->_view->title = $this->_title;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            if(empty($Post)){
                $this->redirect ( '/entrust' );
            }

            if(empty($Post['content'])){
                $this->redirect ( '/entrust' );
                die();
            }

            if(empty($Post['mobile']) || !Tools::isMobile($Post['mobile'])){
                $this->redirect ( '/entrust' );
                die();
            }

            $user_id = empty($this->_logininfo['id']) ? '0' : $this->_logininfo['id'];
            $data =array(
                'demand' =>  $Post['content'],
                'phone' => $Post['mobile'],
                'user_id' => $user_id,
                'create_time' => $this->_daytime,
                'is_back' => 'N',
                'ip' => Tools::getRemoteAddr(),
                'type' => 'buy',
                'status' => '1',
            );

            $demand = new DemandModel();

            $ret = $demand->add($data);
            if(!empty($ret)){
                $this->redirect ( '/entrust/setpfour' );
                die();
            }
        }
    }

    public function setponeAction(){
        $this->_title ="填写基本信息";
        $this->_view->title = $this->_title;
        //委托报价
        $conpanyinfo ='';
        if(!empty($this->_logininfo['id'])){
            $user = new UsersinvoiceModel();
            $conpanyinfo = $user->getRowByWhere("user_id ='{$this->_logininfo['id']}' and 	is_default='Y'");
        }
        $this->_view->conpanyinfo =$conpanyinfo;
    }

    public function setptowAction(){
        $this->_title ="填写货源信息";
        $this->_view->title = $this->_title;
        $Post = $this->_request->getPost();
        if(empty($Post)){
            $this->redirect ( '/entrust/setpone' );
        }
        $this->_view->conpanyinfo =$Post;
    }

    public function setpthreeAction(){
        $this->_title ="委托成功";
        $this->_view->title = $this->_title;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            if(empty($Post)){
                $this->redirect ( '/entrust/setpone' );
            }
            $data =array(
                'demand' =>  $Post['content'],
                'business_name' => $Post['business_name'],
                'phone' => $Post['mobile'],
                'realname' => $Post['contacts'],
                'create_time' => $this->_daytime,
                'ip' => Tools::getRemoteAddr(),
                'type' => 'Q',
                'status' => '1',
            );

            $demand = new DemandModel();

            $ret = $demand->add($data);
            if(empty($ret)){
                dieJs("window.history.back()");
                die();
            }
        }
    }

    public function setpfourAction(){
        $this->_title ="发布成功";
        $this->_view->title = $this->_title;
    }
}
